package com.pdwl.cashoutpay.util;

import com.pdwl.cashoutpay.entity.Merchant;
import com.pdwl.cashoutpay.entity.res.BaseEntity;
import net.sf.json.JSONObject;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class EntityParse {

    @Value("${merchantRegisterUrl}")
    private static String merchantRegisterUrl;

    /*
    回调地址
     */
    @Value("${merchantNotifyUrl}")
    private  static  String merchantNotifyUrl;
    

    public  static Map<String, String> httpServletRequestToMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                System.out.println(name + "       " + valueStr);
            }
            params.put(name, valueStr);
        }
        return params;
    }

    /**
     * byte数组转为string
     *
     * @param encrytpByte
     * @return
     */
    public static String bytesToString(byte[] encrytpByte) {
        String result = "";
        for (Byte bytes : encrytpByte) {
            result += (char) bytes.intValue();
        }
        return result;
    }

    /**
     * 将http请求转换成标准请求类
     *
     * @param httpReq
     * @return
     */

//    public static Merchant httpPayToMerchant(HttpServletRequest httpReq) throws Exception {
//        JSONObject json = JsonUtil.getJson(httpReq);
//        LogUtils.logInfo("注册商户信息：" + json.toString());
//
//
//        //将json请求转为标准外部类
//        Merchant merchant = new Merchant();
//        String outMerchantCode = json.get("merchCode") + TimeUtil.getTime("yyyyMMddHHmmss");//返回的商户号
//        merchant.setOutmerchantcode(outMerchantCode);
//        merchant.setMerchcode("" + json.get("merchCode"));
//        merchant.setSubmerchantcode("");
//        merchant.setMerchname("" + json.get("accName") + "科技有限公司");
//        merchant.setNickname("" + json.get("accName") + "科技");
//        merchant.setMercType("M");
//        merchant.setpMercId("" + json.get("pMercId"));
//        merchant.setServicephone("" + json.get("contactMobile"));
//        merchant.setContacts("" + json.get("accName"));
//        merchant.setContactstel("" + json.get("contactMobile"));
//        merchant.setContactmobile("" + json.get("contactMobile"));
//        merchant.setContactadress("" + json.get("contactAdress"));
//        merchant.setCategorycode("205");
//        merchant.setAccno("" + json.get("accNo"));
//        merchant.setAccname("" + json.get("accName"));
//        merchant.setAcctype("D");
//        merchant.setOpenbank("" + json.get("openBank"));
//        merchant.setAlliedbankcode("" + json.get("alliedBankCode"));
//        merchant.setBankcode("" + json.get("bankCode"));
//        merchant.setTraderate("" + json.get("tradeRate"));
//        merchant.setRemitfee("" + json.get("remitFee"));
//        merchant.setCertno("" + json.get("certNo"));
//        merchant.setTraderate("" + json.get("tradeRate"));
//        merchant.setRemark("" + json.get("merchantNotifyUrl"));//回调地址
//        return merchant;
//    }
//
//
//    public static BaseEntity merchantRegister(Merchant merchant) throws JSONException {
//        BaseEntity baseEntity = new BaseEntity();
//        //商户报备信息
//        Map payReq = new HashMap<String, String>();
//        payReq.put("merchName", merchant.getMerchname()); //商户全称
//        payReq.put("nickName", merchant.getNickname()); //商户简称
//        payReq.put("servicePhone", merchant.getServicephone()); //客服电话
//        payReq.put("contacts", merchant.getContactstel()); //联系人名称
//        payReq.put("contactsTel", merchant.getContactstel()); //联系人电话
//        payReq.put("contactMobile", merchant.getContactmobile()); //联系人手机号
//        payReq.put("contactAdress", merchant.getContactadress()); //联系人地址
//        payReq.put("categoryCode", "205"); //经营类目
//        payReq.put("remark", "1"); //备注
//        payReq.put("merchantCode", ContanGobal.OURMERCHANTCODE); //分配给我们的商户
//        payReq.put("outMerchantCode", merchant.getOutmerchantcode()); //接入商二级商户号
//        payReq.put("accNo", merchant.getAccno()); //银行卡卡号
//        payReq.put("accName", merchant.getAccname()); //银行卡的开户人姓名
//        payReq.put("accType", "D"); //账户类型
//        payReq.put("openBank", merchant.getOpenbank()); //开户行
//        payReq.put("bankCode", merchant.getBankcode()); //开户行简称
//        payReq.put("alliedBankCode", merchant.getAlliedbankcode()); //联行号
//        payReq.put("tradeRate", merchant.getTraderate()); //商户费率
//        payReq.put("remitFee", "0"); //出款手续费（每笔）
//        payReq.put("settlePeriod", "T0"); //结算周期
//        payReq.put("merchantNotifyUrl", merchantNotifyUrl); //商户通知url
//        payReq.put("certNo", "" + merchant.getCertno()); //证件号
//        payReq.put("fastpayPointsTradeRate", merchant.getTraderate()); //快捷费率
//
//        String registerRes = HttpUtil.formUpload(merchantRegisterUrl, payReq);
//        LogUtils.logInfo("收到数据" + registerRes);
//        org.json.JSONObject jsonRes = new org.json.JSONObject(registerRes);
//        String error_code = (String) jsonRes.getString("error_code");
//        String error_message = (String) jsonRes.getString("error_message");
//        if ("0".equals(error_code)) {
//            baseEntity.setError_code("1");
//            baseEntity.setError_message(error_message);
//        } else {
//            baseEntity.setError_code("2");
//            baseEntity.setError_message(error_message);
//        }
//        return baseEntity;
//    }
}
